package br.edu.unirn.dao;

import java.util.List;

import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

import br.edu.unirn.model.Reserva;

public class ReservaDAO extends GenericDAO<Reserva> {

	@Override
	public Class<Reserva> getClassType() {
		return Reserva.class;
	}

	public List<Object[]> findAllReservaByHospedeAndPreco() {
		CriteriaBuilder cb = Database.getInstance().getCriteriaBuilder();
		CriteriaQuery<Object[]> c = cb.createQuery(Object[].class);
		Root<Reserva> r = c.from(Reserva.class);
		c.multiselect(r.<String> get("hospede"), r.<Double> get("preco"));
		
		TypedQuery<Object[]> query = Database.getInstance().createQuery(c);
		List<Object[]> resultado = query.getResultList();
		
		return resultado;
	}
	
	public Double findReservaByPrecoTotal() {
		CriteriaBuilder cb = Database.getInstance().getCriteriaBuilder();
		CriteriaQuery<Double> c = cb.createQuery(Double.class);
		Root<Reserva> r = c.from(Reserva.class);
		c.select(cb.sum(r.<Double>get("preco")));
		
		TypedQuery<Double> query = Database.getInstance().createQuery(c);
		return query.getSingleResult();
	}
}
